package com.useeinfo.oa.modules.base.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.dao.PlanMessageSendDao;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.PlanMessageSend;
import com.useeinfo.oa.modules.base.enums.MessageTypeEnum;
import com.useeinfo.oa.modules.base.vo.MessageVO;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrder;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import com.useeinfo.oa.modules.repair.entity.RepairOrder;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.biz.UserDeviceBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.entity.UserDevice;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import com.useeinfo.oa.modules.threadUtil.ThreadUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: xudong.sun
 * @Date: 2019/1/18 09:18
 * @Description:
 */
@Service("planMessageSendBiz")
public class PlanMessageSendBiz extends CrudBiz<PlanMessageSendDao, PlanMessageSend> {

    @Autowired
    private MessageBiz messageBiz;

    @Autowired
    private SystemUserBiz systemUserBiz;

    @Autowired
    private UserDeviceBiz userDeviceBiz;
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private RepairOrderBiz repairOrderBiz;

    @Autowired
    private TaskOrderBiz taskOrderBiz;





    public List<PlanMessageSend> cancelPlanMessage(){
        QueryParam queryParam = new QueryParam();
        List<PlanMessageSend> planMessageSends = dao.findList(queryParam);
        for(PlanMessageSend planMessageSend:planMessageSends){
            dao.deleteLogic(planMessageSend);
        }
        return planMessageSends;
    }


    //消息推送

    /**
     *
     * @param id 工单id
     * @param type 工单类型
     * @param enterprise 所属企业
     * @param ids 推送人员
     * @param content 推送内容
     */
    @Transactional
    public void senMsg(Long id, String type, Enterprise enterprise, List<Long> ids, String content){
        String code;
        MessageTypeEnum messageType;
        if ("task".equals(type)){
            code = taskOrderBiz.findModel(id).getSerial();
            messageType = MessageTypeEnum.TASK;
        } else if("repair".equals(type)){
            code=repairOrderBiz.findModel(id).getReportCode();
            messageType= MessageTypeEnum.REPAIR;
        }else if("maintain".equals(type)){
            code=maintainOrderBiz.findModel(id).getOrderCode();
            messageType= MessageTypeEnum.MAINTAIN;
        }else{
            code=inspectionOrderBiz.findModel(id).getOrderCode();
            messageType= MessageTypeEnum.INSPECTION;
        }
        List<UserDevice> userDevices = new ArrayList<>();
        logger.info("消息推送的id为：{}",ids.toString());
        for (Long thisId : ids) {
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("userId", thisId.toString());
            userDevices.addAll(userDeviceBiz.findList(queryParam));
        }

        List<MessageVO> messageVOS = new ArrayList<>();
        for (UserDevice userDevice : userDevices) {
            messageVOS.add(new MessageVO(userDevice.getDeviceType(), userDevice.getDeviceToken()));
        }
        if (CollectionUtils.isNotEmpty(messageVOS)) {
            ThreadUtils.sendDeviceMessages(messageVOS, content, messageType, messageBiz,StringConverters.null2String(enterprise.getId()),code);
        }
    }


}
